<?php
// 测试完整处理流程

// 1. 测试API调用
echo "1. 测试API调用...\n";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost/api/food_ingredient/batchGetDishInfo');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded'
]);

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "HTTP状态码: " . $httpCode . "\n";
echo "API响应:\n" . $response . "\n";

// 解析响应
$data = json_decode($response, true);
if ($data && $data['code'] == 1) {
    echo "API调用成功!\n";
    echo "处理结果:\n";
    echo "- 成功处理菜品数量: " . ($data['data']['success_count'] ?? 0) . "\n";
    echo "- 处理的菜品ID: " . implode(', ', $data['data']['processed_dishes'] ?? []) . "\n";
} else {
    echo "API调用失败: " . ($data['msg'] ?? '未知错误') . "\n";
}

echo "\n========================================\n";

// 2. 验证数据库更新
echo "2. 验证数据库更新...\n";
require_once '../thinkphp/base.php';
use think\Db;

if ($data && $data['code'] == 1 && !empty($data['data']['processed_dishes'])) {
    foreach ($data['data']['processed_dishes'] as $dishId) {
        echo "检查菜品ID: {$dishId}\n";
        
        // 检查菜品信息更新
        $dish = Db::name('food_dish')->where('id', $dishId)->find();
        if ($dish) {
            echo "- ai_called: " . ($dish['ai_called'] ?? '未知') . "\n";
            echo "- cook_time: " . ($dish['cook_time'] ?? '空') . "\n";
            echo "- steps长度: " . strlen($dish['steps'] ?? '') . " 字符\n";
            
            // 检查食材关联
            $ingredientCount = Db::name('food_ingredientrel')->where('dish_id', $dishId)->count();
            echo "- 关联食材数量: {$ingredientCount}\n";
        } else {
            echo "- 未找到菜品\n";
        }
        
        echo "----------------------------------------\n";
    }
}

echo "测试完成!\n";
?>